package com.hubt.service.impl;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hubt.common.PageBean;
import com.hubt.model.PtMenuDirectory;
import com.hubt.model.PtRes;
import com.hubt.model.Res;
import com.hubt.model.ResTree;
import com.hubt.mapper.PtMenuDirectoryMapper;
import com.hubt.mapper.PtResMapper;
import com.hubt.model.PtMenuDirectory;
import com.hubt.service.PtMenuDirectoryService;
import com.hubt.vo.Resvo;

@Service("ptservice")
@Transactional
public class PtMenuDirectoryServiceImpl implements PtMenuDirectoryService {

	@Autowired
	private PtMenuDirectoryMapper ptMenuDirectoryMapper;
	
	@Autowired
	private PtResMapper ptmapper;
	@Override
	public PageBean<Resvo> findRes(int curPage, int pageSize) {
		List<PtMenuDirectory> list=ptMenuDirectoryMapper.findAllByPage((curPage-1)*pageSize,pageSize);
		List<Resvo> reslist= new ArrayList<Resvo>();
		for(PtMenuDirectory res:list) {//封装pageBean
			Resvo resvo = new Resvo();
			resvo.setMenu_id( Integer.parseInt(res.getMenuId()));
			resvo.setDir_name(res.getDirName());
			resvo.setDir_level_number(res.getDirLevelNumber());
			if(res.getParentId().equals("root")) {
				res.setDirName("Root");
				resvo.setParent(res);
			}
			else{
				resvo.setParent(ptMenuDirectoryMapper.selectByPrimaryKey(res.getParentId()));
			}
			resvo.setDir_code(res.getDirCode());
			resvo.setDir_order(res.getDirOrder());
			resvo.setIsitem(res.getIsitem());
			resvo.setLocale(res.getLocale());
			resvo.setRes_uuid( Integer.parseInt(res.getResUuid()));
			if(ptmapper.selectByPrimaryKey(res.getResUuid())==null)
					resvo.setRes_url("null");
			else {
				resvo.setRes_url(ptmapper.selectByPrimaryKey(res.getResUuid()).getResUrl());
			}
			
			reslist.add(resvo);
		}
		PageBean<Resvo> pageBean=new PageBean<Resvo>();
		pageBean.setRows(reslist);
		int total=ptMenuDirectoryMapper.countSize();
		pageBean.setTotal(total);
		pageBean.setPageNumber(curPage);
		pageBean.setPageSize(pageSize);
		pageBean.setTotalPage((total%curPage==0)?(total/curPage):(total/pageSize+1));
		return pageBean;
	}

	@Override
	@Transactional
	public ResTree resTree(String parentId) {
		ResTree resTree=new ResTree();
		resTree.setId(parentId);
		if(parentId.equalsIgnoreCase("root"))
			resTree.setName("root");
		else {
			resTree.setName(ptMenuDirectoryMapper.selectByPrimaryKey(parentId).getDirName());
		}
		Queue<ResTree> queue=new LinkedList<ResTree>();
		queue.add(resTree);
		while(!queue.isEmpty()) {
			ResTree temp=queue.poll();
			List<PtMenuDirectory> list=ptMenuDirectoryMapper.findbyParentId(temp.getId());
			List<ResTree> childrenList=new ArrayList<ResTree>();
			for(PtMenuDirectory r:list) {
				ResTree tree=new ResTree(r.getMenuId(), r.getDirName(), null);
				childrenList.add(tree);
				queue.add(tree);
			}
			temp.setChildren(childrenList);
			if(childrenList.size()>0)
				temp.setHasChildren(true);
		}
		return resTree;
	}

	@Override
	public boolean insert(Res res) {
		// TODO Auto-generated method stub
		PtMenuDirectory ptMenuDirectory=new PtMenuDirectory();
		ptMenuDirectory.setDirCode(res.getDir_code());
		ptMenuDirectory.setDirLevelNumber(res.getDir_level_number());
		ptMenuDirectory.setDirName(res.getDir_name());
		ptMenuDirectory.setIsitem(res.getIsitem());
		ptMenuDirectory.setParentId(res.getParent_id());
		ptMenuDirectory.setDirOrder(res.getDir_order());
		ptMenuDirectory.setMenuId(res.getDir_code());
		ptMenuDirectory.setResUuid(res.getDir_code());
		ptMenuDirectory.setLocale("zh_CN");
		
		PtRes ptRes=new PtRes();
		ptRes.setResUuid(res.getDir_code());
		ptRes.setResUrl(res.getRes_url());
		try {
			ptMenuDirectoryMapper.insert(ptMenuDirectory);
			ptmapper.insert(ptRes);
			return  true;
		} catch (Exception e) {
			return false;
		}
				
	}

	@Override
	@Transactional
	public boolean update(Res res) {
		// TODO Auto-generated method stub
		PtMenuDirectory ptMenuDirectory=new PtMenuDirectory();
		ptMenuDirectory.setDirCode(res.getDir_code());
		ptMenuDirectory.setDirLevelNumber(res.getDir_level_number());
		ptMenuDirectory.setDirName(res.getDir_name());
		ptMenuDirectory.setIsitem(res.getIsitem());
		ptMenuDirectory.setParentId(res.getParent_id());
		ptMenuDirectory.setDirOrder(res.getDir_order());
		ptMenuDirectory.setMenuId(res.getDir_code());
		ptMenuDirectory.setResUuid(res.getDir_code());
		
		PtRes ptRes=new PtRes();
		ptRes.setResUuid(res.getDir_code());
		ptRes.setResUrl(res.getRes_url());
		try {
			ptMenuDirectoryMapper.updateByPrimaryKeySelective(ptMenuDirectory);
			ptmapper.updateByPrimaryKeySelective(ptRes);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			return false;
		}
		
	}

	@Override
	@Transactional
	public boolean delete(String id) {
		// TODO Auto-generated method stub
		try {
			ptmapper.deleteByPrimaryKey(ptMenuDirectoryMapper.selectByPrimaryKey(id).getResUuid());
			ptMenuDirectoryMapper.deleteByPrimaryKey(id);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			return false;
		}
		
	}

	@Override
	public Res getRes(String id) {
		PtMenuDirectory ptMenuDirectory=ptMenuDirectoryMapper.selectByPrimaryKey(id);
		PtRes ptRes=ptmapper.selectByPrimaryKey(ptMenuDirectoryMapper.selectByPrimaryKey(id).getResUuid());
		Res res=new Res();
		res.setDir_code(ptMenuDirectory.getDirCode());
		res.setDir_level_number(ptMenuDirectory.getDirLevelNumber());
		res.setDir_name(ptMenuDirectory.getDirName());
		res.setDir_order(ptMenuDirectory.getDirOrder());
		res.setMenu_id(ptMenuDirectory.getMenuId());
		res.setIsitem(ptMenuDirectory.getIsitem());
		res.setLocale(ptMenuDirectory.getLocale());
		res.setRes_uuid(ptMenuDirectory.getResUuid());
		res.setParent_dir_name(ptMenuDirectoryMapper.selectByPrimaryKey(ptMenuDirectory.getParentId()).getDirName());
		res.setParent_id(ptMenuDirectory.getParentId());
		res.setRes_url(ptRes.getResUrl());
		return res;
	}
}
